package com.gexin.weixin.controller.demo;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import com.gexin.common.AppGlobal;
import com.gexin.common.utils.IdGenerateUtils;
import com.gexin.model.weixin.WeixinAppConfig;
import com.gexin.weixin.controller.base.WxBaseController;
import com.gexin.weixin.interceptor.WexinAuthInterceptor;
import com.gexin.weixin.kit.WxConfigKit;
import com.gexin.weixin.kit.XMLUtil;
import com.jfinal.aop.Before;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.log.Log;
import com.jfinal.weixin.sdk.api.PaymentApi;
import com.jfinal.weixin.sdk.api.PaymentApi.TradeType;
import com.jfinal.weixin.sdk.kit.PaymentKit;

@ControllerBind(controllerKey = "/weixin/pay/demo", viewPath = "WEB-INF/extend/weixin/")
public class WexinPayDemoController extends WxBaseController {
	private static final Log log = Log.getLog(WexinPayDemoController.class);

	@Before(WexinAuthInterceptor.class)
	public void index() {
		log.info("支付页面");
		// 1.注意授权目录我设置了 /weixin/pay/ 所以说控制器必须是/weixin/pay/开头
		Map<String, String>  sign_map = sign_pay(IdGenerateUtils.getId(),getLoginUserOpenid());
		for (Map.Entry<String, String> entry : sign_map.entrySet()){
			setAttr(entry.getKey(), entry.getValue());
		}
		render("pay_demo.html");
	}
	
	//演示ajax 获取微信签名参数
	public void ajax_get(){
		Map<String, String>  sign_map = sign_pay(IdGenerateUtils.getId(),getLoginUserOpenid());
		success(sign_map);
	}

	// 签名
	public static Map<String, String> sign_pay(String order_id, String openid) {
		double money = 0.1;
		String notify_url = AppGlobal.Host_Domain_Name + "/weixin/pay/notify"; // 设置回调的URL
		// String order_id = IdGenerateUtils.getId();
		Integer current_id = 1;// 当前要签名的用户id
		WeixinAppConfig config = WxConfigKit.cache_getConfigById(current_id);
		Map<String, String> params = new HashMap<String, String>();
		params.put("appid", config.getAppId());// 应用ID
		params.put("mch_id", config.getWxMchid()); // 微信商户的ID
		params.put("body", "测试商品支付"); // 设置商品的名称 不要超过50个字符串
		params.put("out_trade_no", order_id);// 生成订单ID
		// 金额 1分钱
		int d = BigDecimal.valueOf(money).multiply(BigDecimal.valueOf(100.00)).intValue();
		params.put("total_fee", d + "");// 支付的金额 以分为单位
		params.put("spbill_create_ip", "127.0.0.1");
		params.put("trade_type", TradeType.JSAPI.name());
		params.put("nonce_str", System.currentTimeMillis() / 1000 + "");
		params.put("notify_url", notify_url); // 设置回调的URL 地址
		params.put("openid", openid); // 设置用户的openid
		String sign = PaymentKit.createSign(params, config.getWxKey());
		params.put("sign", sign);
		String xmlResult = PaymentApi.pushOrder(params);
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> map = XMLUtil.doXMLParse(xmlResult);
			String prepay_id = map.get("prepay_id");
			Map<String, String> packageParams = new HashMap<String, String>();
			packageParams.put("appId", config.getAppId());
			packageParams.put("timeStamp", System.currentTimeMillis() / 1000 + "");
			packageParams.put("nonceStr", System.currentTimeMillis() + "");
			packageParams.put("package", "prepay_id=" + prepay_id);
			packageParams.put("signType", "MD5");
			String packageSign = PaymentKit.createSign(packageParams, config.getWxKey());
			packageParams.put("paySign", packageSign);
			return packageParams;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
